package com.run.guifeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.run.guifeng.dao.OrderItemMapper;
import com.run.guifeng.dao.OrderMapper;
import com.run.guifeng.model.entity.Order;
import com.run.guifeng.model.entity.OrderItem;
import com.run.guifeng.model.enums.ServiceResultEnum;
import com.run.guifeng.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderItemMapper orderItemMapper;


    @Override
    public List<Order> getOrders() {
        return orderMapper.selectList(Wrappers.<Order>lambdaQuery().eq(Order::getDeleted, false));
    }

    @Override
    public String updateOrderInfo(Order order) {
        Order temp = orderMapper.selectById(order.getId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        } else {
            order.setCreateTime(temp.getCreateTime());
            return ServiceResultEnum.SUCCESS.getResult();
        }
    }

    @Override
    public String updateOrderInfo(String orderNo, Integer orderStatus) {
        Order order=orderMapper.selectOne(new QueryWrapper<Order>().eq("order_no",orderNo));
        if(order == null){
            return ServiceResultEnum.ORDER_NOT_EXIST_ERROR.getResult();
        }
        else{
            order.setOrderStatus(orderStatus);
            orderMapper.updateById(order);
            return ServiceResultEnum.SUCCESS.getResult();
        }
    }

    @Override
    @Transactional
    public String checkDone(Long[] ids) {
        return null;
    }

    @Override
    public String checkOut(Long[] ids) {
        return null;
    }

    @Override
    public String closeOrder(Long[] ids) {
        return null;
    }

    @Override
    public String saveOrder(Order order) {
        orderMapper.insert(order);
        return null;
    }

    @Override
    public void saveOrderItem(List<OrderItem> orderItems){
        for (OrderItem orderItem : orderItems) {
            orderItemMapper.insert(orderItem);
        }
    }
    @Override
    public Order getOrderDetailByOrderNo(String orderNo, Long userId) {

        Order order = orderMapper.selectOne(
                new QueryWrapper<Order>()
                        .eq("order_no", orderNo)
                        .eq("user_id", userId)
        );
        return order;
    }

    @Override
    public Order getOrderByOrderNo(String orderNo) {
        Order order_no = orderMapper.selectOne(new QueryWrapper<Order>().eq("order_no", orderNo));
        return order_no;
    }


    @Override
    public String cancelOrder(String orderNo, Integer userId) {
        return null;
    }

    @Override
    public String finishOrder(String orderNo, Integer userId) {
        return null;
    }


    @Override
    public List<Order> getMyOrders(Integer userid) {
        return  orderMapper.selectList(Wrappers.<Order>lambdaQuery().eq(Order::getUserId, userid));
    }


    /**
     * 支付成功后，调用该方法，修改支付订单支付状态
     * @param orderNo
     * @param payType
     * @return
     */
    @Override
    public String paySuccess(String orderNo, int payType) {
        Map<String,Object> map = new HashMap<>();
        map.put("orderNo",orderNo);
        map.put("payType",payType);
        orderMapper.updatePayTypeByOrderNo(map);
        return "success";
    }

    @Override
    public List<OrderItem> getOrderItems(String orderid) {
        List<OrderItem> orderItems = orderItemMapper.selectList(
                new QueryWrapper<OrderItem>()
                        .eq("order_no", orderid)
        );
        return orderItems;
    }



    @Override
    public List<Order> getMyOrdersByStatus(Integer userid, Integer status) {
        List<Order> orderpage = orderMapper.selectList(
                new QueryWrapper<Order>()
                        .eq("order_status", status)
                        .eq("user_id",userid)
        );
        return orderpage;
    }

    @Override
    public List<Order> getMyOrdersList(String userid) {
        List<Order> list = orderMapper.selectList(
                new QueryWrapper<Order>()
                        .eq("user_id", userid)
        );
        return list;
    }

    @Override
    public List<Order> searchOrdersByStatus(Integer status) {
        List<Order> orderpage = orderMapper.selectList(
                new QueryWrapper<Order>()
                        .eq("order_status", status)
        );
        return orderpage;
    }
}
